/* 爬虫类便捷函数封装 */
import {getDistance} from '@/utils'
export default class ConvenientExtension extends Creep {
    // 工作状态接口 2状态
    public workstate(rType:ResourceConstant = RESOURCE_ENERGY):void
    {
        if (!this.memory.working) this.memory.working = false;
        if(this.memory.working && this.store[rType] == 0 ) {
            this.memory.working = false;
        }
        if(!this.memory.working && this.store.getFreeCapacity() == 0) {
            this.memory.working = true;
        }
    }

    // 将距离控制器较远的source纳入爬虫的sourceID
    public farsourceID():void
    {
        if (!this.memory.sourceId){
            // 如果没有sourceID,我们需要获取我们要开采的点的sourceID
            var sources = this.room.find(FIND_SOURCES)
            if (sources.length <= 0) this.memory.sourceId = 'no source'
            if (sources.length == 1) this.memory.sourceId = sources[0].id
            if (sources.length >= 2){
                var closest_source = sources[0]
                if (getDistance(sources[1].pos,this.room.controller.pos) > getDistance(sources[0].pos,this.room.controller.pos))
                closest_source = sources[1]
                this.memory.sourceId = closest_source.id
            }
        }
    }
    // 返回距离控制器较远的source
    public farsource():Source | undefined
    {
        var sources = this.room.find(FIND_SOURCES)
        if (sources.length <= 0) return undefined
        if (sources.length == 1) return sources[0]
        if (sources.length >= 2){
            var closest_source = this.room.controller.pos.findClosestByRange(FIND_SOURCES)
            for (var index of sources)
            {
                if (index != closest_source) return index
            }
        }
    }

    // 获取最近的storage或者terminal或者Link或者container，且存量大于自己的搬运度
    public gainStoreStructure(sType:STRUCTURE_CONTAINER|STRUCTURE_STORAGE|STRUCTURE_LINK|STRUCTURE_TERMINAL,rType:ResourceConstant):StructureStorage|StructureContainer|StructureTerminal|StructureLink | undefined{
        return this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(structure)=>{
            return structure.structureType == sType && structure.store.getUsedCapacity(rType) >= this.store.getCapacity()
        }}) as StructureStorage|StructureContainer|StructureTerminal|StructureLink | undefined
    }
    

    /*
        运输函数 C型1号，进行两点间的能量运输 
    */
    public carry_C1(source_:Tank,distination_:Tank,rType:ResourceConstant):void
    {
        this.workstate(rType)
        if (this.memory.working)
        {
            this.transfer__(distination_,rType)
        }
        else
        {
            this.withdraw__(source_,rType)
        }
    }

    /* 去除无关资源 */
    public wipeResource(rType:ResourceConstant,stru:StructureStorage):void
    {
        var st = this.store
        if (Object.keys(st).length > 0)
        for (var i of Object.keys(st))
        {
            if (i != rType)
            {
                this.transfer__(stru,i as ResourceConstant)
                return
            }
        }
    }
}